/**
 * Created by Ryan on 2015/12/5.
 */
$(function () {
    var emailPattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

    $("#loginBtn").click(function () {
        var email = $("#inputEmail").val();
        var password = $("#inputPassword").val();
        var noProblem = true;

        if (email == "") {
            $("#loginEmailError").html("请输入邮箱！").css("display", "block");
            noProblem = false;
        }else if (!emailPattern.test(email)) {
            $("#loginEmailError").html("邮箱格式不正确！").css("display", "block");
            noProblem = false;
        }
        if (password == "") {
            $("#loginPwdError").html("请输入密码！").css("display", "block");
            noProblem = false;
        }else if (password.length < 6) {
            $("#loginPwdError").html("密码长度不能少于6位！").css("display", "block");
            noProblem = false;
        }

        if (noProblem) {
            $.post("login", {"email": email, "password": password}, function (data) {
                if (data == "false") {
                    $("#epError").css("display", "block");
                } else {
                    $("#btn-login, #btn-reg, .div-login, .zhezhao").css("display", "none");
                    $("#btn-nick, #btn-logout, #btn-acc").css("display", "block");
                    $("#btn-nick").html(data);
                }
            });
        }
    });

    $("#regBtn").click(function () {
        var email = $("#email").val();
        var pwd = $("#pwd").val();
        var conPwd = $("#conPwd").val();
        var vCode = $("#vCode").val();
        var noProblem = true;

        if (email == "") {
            $("#regEmailError").html("请输入邮箱！").css("display", "block");
            noProblem = false;
        } else if (!emailPattern.test(email)) {
            $("#regEmailError").html("邮箱格式不正确！").css("display", "block");
            noProblem = false;
        }
        if (pwd == "") {
            $("#regPwdError").html("请输入密码！").css("display", "block");
            $("#conPwd").val("");
            noProblem = false;
        }else if (pwd.length < 6) {
            $("#regPwdError").html("密码长度不能少于6位！").css("display", "block");
            $("#conPwd").val("");
            noProblem = false;
        }else if (conPwd == "") {
            $("#conPwdError").html("请再次输入密码！").css("display", "block");
            noProblem = false;
        } else if (conPwd != pwd) {
            $("#conPwdError").html("两次输入的密码不一致！").css("display", "block");
            noProblem = false;
        }

        if (vCode == "") {
            $("#vCodeError").html("请输入验证码！").css("display", "block");
            noProblem = false;
        }

        if(noProblem){
            $.post("register", {"email": email, "password": pwd,"vCode": vcode}, function (data){

            });
        }
    });
});